package com.x.Part2_Exception类.Throw;

/**
 * ClassName: Demo
 * Package: IntelliJ IDEA
 * Description:
 * Written by HanXu
 * Create 2024.03.18 14:46
 */
public class Demo {
    public static void main(String[] args) {

        int[] arr = {};
        int max;
        arr = null;

        try {
            max = getMax(arr);
        } catch (NullPointerException e) {
            System.out.println("空指针异常");
            throw new NullPointerException();
        } catch (ArrayIndexOutOfBoundsException e) {
            System.out.println("越界");
            throw new ArrayIndexOutOfBoundsException();
        }
        System.out.println(max);


    }

    public static int getMax ( int[] arr){

        if (arr == null)
            throw new NullPointerException();

        if (arr.length == 0)
            throw new ArrayIndexOutOfBoundsException();

        int max = arr[0];
        for (int i = 0; i < arr.length - 1; i++) {
            if (arr[i + 1] > max)
                max = arr[i + 1];
        }
        return max;
    }
}
